package ex7;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

interface IntegerStack {
	public Integer push(Integer item);
	public Integer pop();
	public Integer peek();
	public boolean empty();
	public int size();
}


class ArrayListIntegerStack implements IntegerStack {

	private ArrayList<Integer> arrayList;
	
	
	public ArrayListIntegerStack() {
		arrayList = new ArrayList<Integer>();
	}
	
	@Override
	public Integer push(Integer item) {
		if (item == null) {
			return null;
		} else {
			arrayList.add(item);
			return item;
		}
		
	}

	@Override
	public Integer pop() {
		if (arrayList.isEmpty()) {
			return null;
		} else {
			Integer integer = arrayList.get(arrayList.size() - 1);
			arrayList.remove(arrayList.size() - 1);
			return integer;
		}
	}

	@Override
	public Integer peek() {
		if (arrayList.isEmpty()) {
			return null;
		} else {
			Integer integer = arrayList.get(arrayList.size() - 1);
			return integer;
		}
	}

	@Override
	public boolean empty() {
		return arrayList.isEmpty();
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return arrayList.size();
	}

	@Override
	public String toString() {
		return arrayList.toString();
	}
	
	
	
}

public class Test1 {
	public static void main(String[] args) {
		ArrayListIntegerStack arrayListIntegerStack = new ArrayListIntegerStack();
		Scanner scanner = new Scanner(System.in);
		int m = scanner.nextInt();
		for (int i = 0; i < m; i++) {
			System.out.println(arrayListIntegerStack.push(scanner.nextInt()));
		}
		
		System.out.println(arrayListIntegerStack.peek() + ","
				+ arrayListIntegerStack.empty() + ","
				+ arrayListIntegerStack.size());
		
		System.out.println(arrayListIntegerStack);
		
		int x = scanner.nextInt();
		for (int i = 0; i < x; i++) {
			System.out.println(arrayListIntegerStack.pop());
		}
		
		System.out.println(arrayListIntegerStack.peek() + ","
				+ arrayListIntegerStack.empty() + ","
				+ arrayListIntegerStack.size());
		
		System.out.println(arrayListIntegerStack);
		
		
		
		scanner.close();
	}
	
	
	
	
	
	
}